package com.example.studyllm.service.rag;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyllm.entity.rag.RagModel;
import com.example.studyllm.mapper.rag.RagModelMapper;
import com.example.studyllm.service.base.BaseServiceImpl;
import com.example.studyllm.utils.BeanUtils;
import com.example.studyllm.vo.base.PageResultVO;
import com.example.studyllm.vo.base.SelectVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *   service实现类
 * </p>
 *
 * @author kw
 * @since 2025-02-05 15:12:50.850253
 */
@Service
public class RagModelService extends BaseServiceImpl<RagModelMapper, RagModel> {


    public Boolean addRagModel(RagModel ragModel) {
        ragModel.setCode(ragModel.getSupplier()+ragModel.getType()+UUID.randomUUID());
        return this.save(ragModel);
    }

    public Boolean updateRagModelById(RagModel ragModel) {
        return this.updateById(ragModel);
    }

    public PageResultVO<RagModel> getPage(RagModel pageParam) {

        LambdaQueryWrapper<RagModel> lambda = new QueryWrapper<RagModel>().lambda();
        this.buildCondition(lambda, pageParam);
        lambda.orderBy(true, false, RagModel::getId);                  
        PageResultVO<RagModel> resultPage = BeanUtils.changePage(RagModel.class, this.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda));
        return resultPage;
    }
    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<RagModel> lambda, RagModel param) {
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getId()),RagModel::getId, param.getId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCode()),RagModel::getCode, param.getCode());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getModelName()),RagModel::getModelName, param.getModelName());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getType()),RagModel::getType, param.getType());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getName()),RagModel::getName, param.getName());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getSupplier()),RagModel::getSupplier, param.getSupplier());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getResponseLength()),RagModel::getResponseLength, param.getResponseLength());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getTemperature()),RagModel::getTemperature, param.getTemperature());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getTop()),RagModel::getTop, param.getTop());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getApiKey()),RagModel::getApiKey, param.getApiKey());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getSecretKey()),RagModel::getSecretKey, param.getSecretKey());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUrl()),RagModel::getUrl, param.getUrl());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDimensions()),RagModel::getDimensions, param.getDimensions());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCtime()),RagModel::getCtime, param.getCtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUtime()),RagModel::getUtime, param.getUtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDeleteFlag()),RagModel::getDeleteFlag, param.getDeleteFlag());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCby()),RagModel::getCby, param.getCby());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUby()),RagModel::getUby, param.getUby());
        
    }

    public List<SelectVO> groupBySupplier() {
        LambdaQueryWrapper<RagModel> lambda = new QueryWrapper<RagModel>().lambda();
        lambda.eq(RagModel::getDeleteFlag,0);
        lambda.eq(RagModel::getType,"MODEL");
        List<RagModel> ragModels = list(lambda);
        List<SelectVO> selectVOS = new ArrayList<>(ragModels.size());

// 1. 按supplier分组（假设RagModel有getSupplierId()和getSupplierName()方法）
        Map<String, List<RagModel>> grouped = ragModels.stream()
                .collect(Collectors.groupingBy(RagModel::getSupplier));

// 2. 转换为SelectVO结构
        for (Map.Entry<String, List<RagModel>> entry : grouped.entrySet()) {
            String supplierId = entry.getKey();
            // 获取供应商名称（假设所有同组的supplierName一致）
            String supplierName = entry.getValue().get(0).getSupplier();

            SelectVO parent = new SelectVO();
            parent.setId(supplierId);
            parent.setName(supplierName);
            parent.setChild(new ArrayList<>());

            // 转换子节点
            List<SelectVO> children = entry.getValue().stream()
                    .map(ragModel -> {
                        SelectVO child = new SelectVO();
                        child.setId(String.valueOf(ragModel.getId()));
                        child.setName(ragModel.getName());
                        return child;
                    }).collect(Collectors.toList());

            parent.setChild(children);
            selectVOS.add(parent);
        }

        return selectVOS;
    }
}